package com.jkoa;

import able.stripes.JSP;

import com.google.inject.Inject;
import com.jkoa.mgr.UserManager;
import com.wideplay.warp.persist.Transactional;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationMethod;

@UrlBinding("/login")
public class LoginActionBean extends WelcomeActionBean {
   
    @Validate(required=true)
    private String userLogin;
    @Validate(required=true)
    private String userPassword;    
    @Inject
    private UserManager userManager;
    

    @DefaultHandler
    @DontValidate
    public Resolution showForm() {
        return new JSP("simple_login.jsp");
    } 
    
    @ValidationMethod
    @Transactional
    public void validate_login() {
        if(!userManager.activate(userLogin)){
            addError("user.userId", "user", "activate");
            return;
        }
        if (!userManager.authenticate(getUserLogin(), getUserPassword())) {
            addError("user.userId", "user", "badLogin");
        }
    }

    @Transactional
    public Resolution login() {
        getContext().setCurrentUser(userManager.findByUserId(getUserLogin()));
        return new RedirectResolution(HomeActionBean.class);
    }    
   
    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    
}
